package com.github.llyb120.calengine.parser;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.llyb120.calengine.rule.RuleEngine;
import com.github.llyb120.performance.define.Generator;
import com.github.llyb120.performance.test.All;
import com.github.llyb120.performance.test.Field;
import com.github.llyb120.performance.test.Generate;
import lombok.Data;
import lombok.SneakyThrows;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import javax.script.ScriptEngine;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;

public class GeneratorParser extends AbstractParser {


    @Data
    static class GeneratorOption {
        @Option(name = "-s", required = false, usage = "源文件夹")
        String src;
        @Option(name = "-t", required = false, usage = "目标文件夹")
        String target;
    }


    @SneakyThrows
    @Override
    public void parse(String[] args) {
        GeneratorOption option = new GeneratorOption();
        CmdLineParser parser = new CmdLineParser(option);
        parser.parseArgument(args);
        if(parser.getOptions().size() == 2){
            generate(option.src, option.target);
        } else {
            parser.printUsage(System.out);
        }
    }


    public void generate(String from, String target) {
        RuleEngine ruleEngine = new RuleEngine();
        FileUtil.walkFiles(Paths.get(from), 10, new SimpleFileVisitor<Path>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if(file.toString().endsWith(".groovy")){
                    generate(ruleEngine, file, target);
                }
                return super.visitFile(file, attrs);
            }
        });

    }

    @SneakyThrows
    private void generate(RuleEngine ruleEngine, Path file, String target) {
//        String code = FileUtil.readUtf8String(file.toFile());
//        ScriptEngine scriptEngine = ruleEngine.getScriptEngine();
//        List list = (List) All.getElements();
//        list.clear();
//        scriptEngine.eval(code);
//        for (Object o : list) {
//            if (!(o instanceof Generate)) {
//                continue;
//            }
//            List result = new ArrayList();
//            for (int i = 0; i < ((Generate) o).getNum(); i++) {
//                Map item = new LinkedHashMap();
//                for (Field field : ((Generate) o).getField()) {
//                    field.getParams().put("_pos", i);
//                    Generator generator =  ruleEngine.getRule(RuleEngine.GENERATOR, field.getGenerator());
//                    item.put(field.getLabel(), generator.getHandler().call(field.getParams()));
//                }
//                result.add(item);
//                System.out.println(item);
//            }
//            File targetFile = new File(target, ((Generate) o).getFileName());
//            if(!targetFile.getParentFile().exists()){
//                targetFile.getParentFile().mkdirs();
//            }
//            ExcelWriter writer = ExcelUtil.getWriter(targetFile);
//            writer.autoSizeColumnAll();
//            writer.write(result, true);
//            writer.flush();
//            writer.close();
//        }
    }

}
